package cn.audit.controller.zqmk;

import cn.audit.bean.pojo.Property;
import cn.audit.bean.pojo.PropertyList;
import cn.audit.repository.zqmk.PropertyListRepository;
import cn.audit.repository.zqmk.PropertyRepository;
import cn.audit.service.zqmk.PropertyListService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 钟琪
 * @Date: 2019/5/20 15:10
 */
@Controller
@Transactional
public class ModelOneController {
    @Resource
    private PropertyRepository propertyRepository;

    @Resource
    private PropertyListRepository propertyListRepository;

    @Resource
    private PropertyListService propertyListService;

    @RequestMapping("/prop")
    public String prop(Model model){
        List<Property> propertyList=propertyRepository.findAll();
        System.out.println(propertyList);
        model.addAttribute("prop",propertyList);
        return "/property";
    }

    @ResponseBody
    @RequestMapping("/list")
    public List<PropertyList> list(Integer id){
        System.out.println(id+"id");
       List<PropertyList> propertyList=propertyListService.findAllByPropertyid(id);
        System.out.println(propertyList);

        return propertyList;
    }

    @RequestMapping("/propertyadd")
    public String propertyadd(Property p){
        System.out.println(p.toString()+"pppppppppppppp");
        p.setJurisdiction(0);
        propertyRepository.save(p);
        return "redirect:/prop";
    }

    @ResponseBody
    @RequestMapping("/PropertyUpdate")
    public Property PropertyUpdate(Integer id){
     Optional<Property> optional= propertyRepository.findById(id);
     Property property=optional.get();
     return property;
    }


    @RequestMapping("/propertysave")
    public String propertysave(Property property){
        propertyRepository.save(property);
        return "redirect:/prop";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        System.out.println(id);
        System.out.println(id);
        System.out.println(id);
        System.out.println(id);System.out.println(id);System.out.println(id);System.out.println(id);System.out.println(id);
        propertyListService.deleteByPropertyid(id);
        propertyRepository.deleteById(id);
        return "redirect:/prop";
    }


    @RequestMapping("/propertylistadd")
    public String propertylistadd(PropertyList propertyList){
        propertyListRepository.save(propertyList);
        System.out.println(propertyList);
        return "redirect:/prop";
    }

    @ResponseBody
    @RequestMapping("/listupdate")
    public List<PropertyList> listupdate(Integer id){
        List<PropertyList> propertyListList=propertyListRepository.findAllByPropertyid(id);
        System.out.println(propertyListList);
        return propertyListList;

    }


    @ResponseBody
    @RequestMapping("/listsave")
    public PropertyList listsave(Integer id){
        Optional<PropertyList> optionalPropertyList=propertyListRepository.findById(id);
        PropertyList propertyList=optionalPropertyList.get();
        return propertyList;
    }

    @RequestMapping("/listsaves")
    public String listsaves(PropertyList propertyList){
        propertyListRepository.save(propertyList);
        return "redirect:/prop";
    }

    @RequestMapping("/listdel")
    public String listdel(Integer id){
        propertyListRepository.deleteById(id);
        return "redirect:/prop";
    }
 }
